package com.wy.config;

import com.wy.utils.Constant;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @描述
 * @创建人 wangyue
 * @创建时间2019/12/1210:57
 */
@Configuration
public class RabbitMqConfig{
    //简单队列
    @Bean
    public Queue redirectQueue() {
        return new Queue(Constant.DIRECT_QUEUE);
    }
    //work队列 （和简单一样）
    @Bean
    public Queue workQueue() {
        return new Queue(Constant.WORK_QUEUE);
    }
    //fanout模式（基础的发布订阅--分裂模式）
    @Bean
    public Queue AMessage() {
        return new Queue("fanout.A");
    }

    @Bean
    public Queue BMessage() {
        return new Queue("fanout.B");
    }

    @Bean
    public Queue CMessage() {
        return new Queue("fanout.C");
    }

    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    @Bean
    Binding bindingExchangeA(Queue AMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(AMessage).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(BMessage).to(fanoutExchange);
    }

    @Bean
    Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(CMessage).to(fanoutExchange);
    }
    //routing路由模式，每个队列往交换器上绑定时，需要指定一个路由key
    @Bean
    public Queue routA() {
        return new Queue("topic.A");
    }

    @Bean
    public Queue routB() {
        return new Queue("topic.B");
    }
    //交换机
    @Bean
    TopicExchange exchange() {
        return new TopicExchange("topicExchange");
    }
    @Bean
    Binding bindingExchangeMessageA(Queue routA, TopicExchange exchange) {
        return BindingBuilder.bind(routA).to(exchange).with("topic.keyA");
    }
    @Bean
    Binding bindingExchangeMessageB(Queue routB, TopicExchange exchange) {
        return BindingBuilder.bind(routB).to(exchange).with("topic.keyB");
    }
    //topic模式，模糊匹配key
    @Bean
    Binding bindingExchangeMessages(Queue routA, TopicExchange exchange) {
        return BindingBuilder.bind(routA).to(exchange).with("topic.#");
    }
}
